package com.bw.wxy.mvp.view;

import android.Manifest;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.blankj.utilcode.util.BarUtils;
import com.bw.wxy.R;
import com.bw.wxy.common.SpUtils;
import com.bw.wxy.fragment.Fragment_live;
import com.bw.wxy.fragment.Fragment_news;
import com.bw.wxy.fragment.Fragment_pople;
import com.bw.wxy.fragment.Fragment_video;
import com.bw.wxy.fragment.UserFragment;
import com.bw.wxy.mvp.base.view.BaseActivity;
import com.bw.wxy.widget.BottomBut;

import org.greenrobot.eventbus.EventBus;

public class BottomActivity extends AppCompatActivity implements BottomBut.SelectListener {
    private BottomBut newsBottom;
    private BottomBut peopleBottom;
    private BottomBut videoBottom;
    private BottomBut liveBottom;
    private BottomBut mineBottom;

    private Fragment_news  fragment_news;
    private Fragment_pople fragment_pople;
    private Fragment_video fragment_video;
    private Fragment_live fragment_live;
    private UserFragment userFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom);
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            requestPermissions(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA
            },999);
        }
        initView();
        newsBottom.setContent("新闻").setNormalColor(Color.GRAY).setSelColor(Color.RED)
                .setNormalImg(R.drawable.tu1_f).setSelImg(R.drawable.tu1_t)
                .setSelectListener(this::onSelect)
                .setShowPoint(false);
        peopleBottom.setContent("人民号").setNormalColor(Color.GRAY).setSelColor(Color.RED)
                .setNormalImg(R.drawable.tu2_f).setSelImg(R.drawable.tu2_t)
                .setSelectListener(this::onSelect)
                .setShowPoint(false);
        videoBottom.setContent("视频").setNormalColor(Color.GRAY).setSelColor(Color.RED)
                .setNormalImg(R.drawable.tu3_f).setSelImg(R.drawable.tu3_t)
                .setSelectListener(this::onSelect)
                .setShowPoint(false);
        liveBottom.setContent("直播").setNormalColor(Color.GRAY).setSelColor(Color.RED)
                .setNormalImg(R.drawable.tu4_f).setSelImg(R.drawable.tu4_t)
                .setSelectListener(this::onSelect)
                .setShowPoint(false);
        mineBottom.setContent("我的").setNormalColor(Color.GRAY).setSelColor(Color.RED)
                .setNormalImg(R.drawable.tu5_f).setSelImg(R.drawable.tu5_t)
                .setSelectListener(this::onSelect)
                .setShowPoint(false);

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragment_news=new Fragment_news();
        fragmentTransaction.add(R.id.ll_fragment,fragment_news);
        fragmentTransaction.commit();
        onSelect(R.id.news_bottom);
    }

    public void getStatusColor(boolean is){
        if (is==true){
            BarUtils.setStatusBarColor(this, Color.RED);
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }else {
            BarUtils.setStatusBarColor(this, Color.WHITE);
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }


    private void initView() {
        newsBottom = findViewById(R.id.news_bottom);
        peopleBottom = findViewById(R.id.people_bottom);
        videoBottom = findViewById(R.id.video_bottom);
        liveBottom = findViewById(R.id.live_bottom);
        mineBottom = findViewById(R.id.mine_bottom);
    }

    @Override
    public void onSelect(int id) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (fragment_news!=null){
            fragmentTransaction.hide(fragment_news);
        }
        if (fragment_pople!=null){
            fragmentTransaction.hide(fragment_pople);
        }
        if (fragment_live!=null){
            fragmentTransaction.hide(fragment_live);
        }
        if (userFragment!=null){
            fragmentTransaction.hide(userFragment);
        }
        if (fragment_video!=null){
            fragmentTransaction.hide(fragment_video);
        }


        if (id==R.id.news_bottom){
            peopleBottom.clearSelect();
            videoBottom.clearSelect();
            mineBottom.clearSelect();
            mineBottom.clearSelect();
            fragmentTransaction.show(fragment_news);
            getStatusColor(false);
        }else if (id==R.id.people_bottom){
            newsBottom.clearSelect();
            videoBottom.clearSelect();
            liveBottom.clearSelect();
            mineBottom.clearSelect();
            if (fragment_pople==null){
                fragment_pople=new Fragment_pople();
                fragmentTransaction.add(R.id.ll_fragment, fragment_pople);
            }else{
                fragmentTransaction.show(fragment_pople);
            }
            getStatusColor(false);
        }else if (id==R.id.video_bottom){
            newsBottom.clearSelect();
            peopleBottom.clearSelect();
            liveBottom.clearSelect();
            mineBottom.clearSelect();
            if (fragment_video==null){
                fragment_video=new Fragment_video();
                fragmentTransaction.add(R.id.ll_fragment, fragment_video);
            }else{
                fragmentTransaction.show(fragment_video);
            }
            getStatusColor(false);
        }
        else if (id==R.id.live_bottom){
            newsBottom.clearSelect();
            videoBottom.clearSelect();
            peopleBottom.clearSelect();
            mineBottom.clearSelect();
            if (fragment_live == null) {
                fragment_live = new Fragment_live();
                fragmentTransaction.add(R.id.ll_fragment, fragment_live);
            } else {
                fragmentTransaction.show(fragment_live);
            }
            getStatusColor(false);
        }
        else if (id==R.id.mine_bottom){
            newsBottom.clearSelect();
            videoBottom.clearSelect();
            liveBottom.clearSelect();
            peopleBottom.clearSelect();
            if (userFragment == null) {
                if (SpUtils.getLogin()){
                    EventBus.getDefault().postSticky("刷新");
                }
                userFragment = new UserFragment();
                fragmentTransaction.add(R.id.ll_fragment, userFragment);
            } else {
                fragmentTransaction.show(userFragment);
            }
            getStatusColor(true);
        }
        fragmentTransaction.commit();
    }
}